package com.tanhua.sso.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

//配置类注入扫描@Bean
@Configuration
public class RestTemplateConfig {

    @Bean
    //RestTemplate是Spring提供的用于访问Rest服务的客户端
    //ClientHttpRequest它代表请求的客户端，该接口继承自HttpRequest、HttpOutputMessage，
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        RestTemplate restTemplate = new RestTemplate(factory);
        //设置中文编码
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;
    }
    @Bean
    //Spring提供的SimpleClientHttpRequestFactory类，能够构造http request请求
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){

        SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
        httpRequestFactory.setReadTimeout(5000);//单位ms 请求超出时间
        httpRequestFactory.setConnectTimeout(5000);//单位ms,链接超出时间
        return httpRequestFactory;
    }

}
